<template>
  <el-dialog :title="isEdit?'编辑移位':'新增移位'"
             :visible.sync="dialogForm"
             width="1200px">
    <el-form :inline="true"
             :model="condition"
             class="search-form"
             ref="aList"
             label-width="80px">
      <el-row>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="所属仓库">
            <el-select v-model="condition['warehouseName']"
                       placeholder="请选择"
                       @change="changeWarehouse('1')">
              <el-option v-for="(item,index) in warehouseList"
                         :key="index"
                         :label="item.name"
                         :value="item.name">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="库房">
            <el-select v-model="condition['roomName']"
                       placeholder="请选择"
                       clearable
                       @change="changeTargetRoom('1')">
              <el-option v-for="(item,index) in storageSet1"
                         :key="index"
                         :label="item.roomName"
                         :value="item.roomCode"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="跨数">
            <el-select v-model="condition['spanNo']"
                       placeholder="请选择"
                       clearable
                       filterable>
              <el-option v-for="item in ktipArr1"
                         :key="item"
                         :label="item<10?'0'+item : item"
                         :value="item<10?'0'+item : item"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="间数">
            <el-select v-model="condition['aulaNo']"
                       placeholder="请选择"
                       clearable
                       filterable>
              <el-option v-for="item in jtipArr1"
                         :key="item"
                         :label="item<10?'0'+item : item"
                         :value="item<10?'0'+item : item"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="排号">
            <el-input v-model="condition['rowNo']"
                      placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="垛位">
            <el-input v-model="condition['stackNo']"
                      placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="层高">
            <el-input v-model="condition['tierNo']"
                      placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="品名">
            <el-select v-model="condition['cargoName']"
                       placeholder="请选择"
                       clearable
                       filterable>
              <el-option v-for="item in cargoNameList"
                         :key="item.cargoName"
                         :label="item.cargoName"
                         :value="item.cargoName"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="规格1">
            <auto-search-dic @selectName="v=>condition['normOne']=v"
                             :name="condition['normOne']"
                             code="CARGO_GGY"></auto-search-dic>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="规格2">
            <auto-search-dic @selectName="v=>condition['normTwo']=v"
                             :name="condition['normTwo']"
                             code="CARGO_GGY"></auto-search-dic>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="规格3">
            <auto-search-dic @selectName="v=>condition['normThree']=v"
                             :name="condition['normThree']"
                             code="CARGO_GGY"></auto-search-dic>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="生产厂家">
            <template slot-scope="scope">
              <auto-search-dic @selectName="v=>condition['madeBy']=v"
                               :name="condition['madeBy']"
                               code="CARGO_SCZ"></auto-search-dic>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                class="search-width4">
          <el-form-item label="跟踪号">
            <el-input type="text"
                      v-model="condition['search_LIKE_barcode']"
                      placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button type="primary"
                     icon="el-icon-search"
                     class="ml-80"
                     @click="searchClick">查 询</el-button>
          <el-button type="info"
                     icon="el-icon-delete"
                     class="ml-80"
                     @click="handleRemove">移 除</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="lsShiftData"
              border
              stripe
              @selection-change="handleSelectionChange"
              max-height="550">
      <el-table-column type="selection"
                       width="30"></el-table-column>
      <el-table-column label="序号"
                       type="index"
                       width="50"></el-table-column>
      <el-table-column prop="trackingNo"
                       label="跟踪号"
                       min-width="80"></el-table-column>
      <el-table-column prop="baleNo"
                       label="捆包号"
                       min-width="80"></el-table-column>
      <el-table-column prop="warehouseName"
                       label="仓库"
                       min-width="80"></el-table-column>
      <el-table-column prop="roomName"
                       label="库房"
                       min-width="80"></el-table-column>
      <el-table-column prop="roomLocation"
                       label="库位"
                       min-width="120"></el-table-column>
      <el-table-column prop="cargoName"
                       label="品名"
                       min-width="80"></el-table-column>
      <el-table-column prop="normStr"
                       label="规格"
                       min-width="100"></el-table-column>
      <el-table-column prop="material"
                       label="材质"
                       min-width="80"></el-table-column>
      <el-table-column prop="tagWeight"
                       label="吊牌重量"
                       min-width="80"></el-table-column>
      <el-table-column prop="stoveNo"
                       label="炉批号"
                       min-width="80"></el-table-column>
      <el-table-column prop="madeBy"
                       label="生产厂家"
                       min-width="80"></el-table-column>
      <el-table-column prop="inventoryNum"
                       label="件数"
                       min-width="80"></el-table-column>
      <el-table-column prop="pledgedWeight"
                       label="重量"
                       min-width="80"></el-table-column>
      <el-table-column prop="inventoryWeight"
                       label="数量"
                       min-width="80"></el-table-column>
      <el-table-column prop="inventoryId"
                       label="物资ID"
                       min-width="80"></el-table-column>
    </el-table>

    <el-form ref="form"
             :model="form"
             :rules="rules">
      <el-row>
        <el-col>
          <el-form-item label="移位类型:">
            <el-radio-group v-model="form.relocationType"
                            @change="changeRelocationType">
              <el-radio :label="'A'">仓库间调整</el-radio>
              <el-radio :label="'B'">库房间调整</el-radio>
              <el-radio :label="'C'">库房内调整</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>新位置信息</el-row>
      <br />
      <el-row class="row">
        <el-col :span="6">
          <el-form-item label="新仓库"
                        prop="targetWarehouseCode">
            <el-select v-model="form.targetWarehouseCode"
                       placeholder="请选择"
                       clearable
                       :disabled="typeDisabled"
                       @change="changeWarehouse">
              <el-option v-for="(item,index) in warehouseList"
                         :key="index"
                         :label="item.name"
                         :value="item.code">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="新库房"
                        prop="targetRoomCode">
            <el-select v-model="form.targetRoomName"
                       placeholder="请选择"
                       clearable
                       :disabled="typeDisabled1"
                       @change="changeTargetRoom">
              <el-option v-for="(item,index) in storageSet"
                         :key="index"
                         :label="item.roomName"
                         :value="item.roomName"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="新跨数"
                        prop="targetSpan">
            <el-select v-model="form.targetSpan"
                       placeholder="请选择"
                       clearable
                       filterable>
              <el-option v-for="item in ktipArr"
                         :key="item"
                         :label="item<10?'0'+item : item"
                         :value="item<10?'0'+item : item"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="新间数"
                        prop="targetAula">
            <el-select v-model="form.targetAula"
                       placeholder="请选择"
                       clearable
                       filterable>
              <el-option v-for="item in jtipArr"
                         :key="item"
                         :label="item<10?'0'+item : item"
                         :value="item<10?'0'+item : item"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="是否指定内转车:"
                        prop="ifSpecifyCar">
            <el-radio-group v-model="form.ifSpecifyCar">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8"
                v-show="form.ifSpecifyCar===true"
                prop="carNo">
          <el-form-item label="内转车号"
                        prop="carNo">
            <el-input type="text"
                      v-model="form.carNo"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="是否计件:"
                      prop="ifPieceWork">
          <el-radio-group v-model="form.ifPieceWork">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="dialogForm = false">取 消</el-button>
      <el-button class="ml-80"
                 type="primary"
                 @click="handleSubmit"
                 :loading="$store.getters.loadingBtn">保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getCargoNameList, getPiecesCargo } from '@/api/things/accountPageList'
import { detailAudit, deleteAudit } from '@/api/things/auditList' //详情
import {
  addShift,
  getFindInventory,
  updateShift
} from '@/api/matter/shiftSearch'
// import { getAuditList } from '@/api/things/auditList' //审核统计
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoSearchName from '@/components/autocomplete/autoSearchName'
import addShiftForm from './addShiftForm'
import { getWarehouseList } from '@/api/adminWarehouse/adminWarehouse' //所属仓库来源于仓库设置
import { getStorageSetList } from '@/api/storageSet/storageSet' //仓库作业点设置左边表格  所属库房来源于库房设置
import autoSearchDic from '@/components/autocomplete/autoSearchDic'

export default {
  name: 'shiftSearch',
  components: {
    autoSearchOrg,
    autoSearchName,
    addShiftForm,
    autoSearchDic
  },
  data() {
    return {
      form: this.clearForm(),
      rules: {
        targetWarehouseCode: [
          { required: true, message: '请选择新仓库', trigger: 'blur' }
        ],
        targetRoomCode: [
          { required: true, message: '请选择新库房', trigger: 'blur' }
        ],
        targetSpan: [
          { required: true, message: '请选择新跨', trigger: 'blur' }
        ],
        targetAula: [{ required: true, message: '请选择新间', trigger: 'blur' }]
      },
      removeIds: [],
      warehouseList: [],
      storageSet: [],
      storageSet1: [], //搜索仓库
      cargoNameList: [],
      disabled: false,
      dialogForm: false,
      isEdit: false,
      condition: {
        aulaNo: '',
        baleNo: '',
        carNo: '',
        cargoName: '',
        detailId: '',
        endBy: '',
        endJobPointName: '',
        endTime: '',
        inventoryCountNum: '',
        inventoryId: '',
        inventoryNum: '',
        inventoryWeight: '',
        madeBy: '',
        material: '',
        normOne: '',
        normStr: '',
        normThree: '',
        normTwo: '',
        roomLocation: '',
        roomName: '',
        rowNo: '',
        spanNo: '',
        stackNo: '',
        startBy: '',
        startJobPointName: '',
        startTime: '',
        status: '',
        stoveNo: '',
        tagWeight: '',
        targetRoomLocation: '',
        targetRoomName: '',
        targetWarehouseName: '',
        tierNo: '',
        trackingNo: '',
        warehouseName: ''
      }, // 查询条件
      shiftData: [], // table数组
      lsShiftData: [], // table数组
      // pageSearch: getAuditList, // 分页查询api接口
      id: null,
      multipleSelection: [],
      ktipArr: '',
      jtipArr: '',
      ktipArr1: '',
      jtipArr1: '',
      typeDisabled: false,
      typeDisabled1: false
    }
  },
  mounted() {
    //获取仓库
    getWarehouseList().then(res => {
      this.warehouseList = res.data.aaData
    })
    //获取货名
    getCargoNameList(this.$store.state.user.currentUser.org.id).then(res => {
      this.cargoNameList = res.data.aaData
    })
  },
  methods: {
    clearForm() {
      return {
        carNo: '',
        ids: [],
        ifPieceWork: true,
        ifSpecifyCar: true,
        relocationType: 'A',
        targetAula: '',
        targetRoomName: '',
        targetRoomCode: '',
        targetSpan: '',
        targetWarehouseName: '',
        targetWarehouseCode: '',
        targetWarehouseId: '',
        warehouseCode: '',
        warehouseName: ''
      }
    },
    loadData() {
      this.$refs.shiftList.loadData()
    },
    searchClick() {
      let that = this
      that.lsShiftData = that.shiftData
      console.log(that.condition)
      let par = that.filterPamars(that.condition)
      Object.keys(par).forEach(function(key) {
        if (key) {
          that.lsShiftData = that.lsShiftData.filter(
            item => item[key] == par[key]
          )
        }
        console.log(key, par[key])
      })
      that.$nextTick(function() {
        that.lsShiftData
        that.$forceUpdate()
      })
    },
    // 保存
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.handleToEdit() : this.handleToAdd()
        } else {
          this.$message({
            message: '请检查表单',
            type: 'error'
          })
        }
      })
    },
    // 新增
    handleToAdd() {
      let ids = []
      for (var i = 0; i < this.lsShiftData.length; i++) {
        ids.push(this.lsShiftData[i].inventoryId)
      }
      this.form.ids = ids
      addShift(this.form).then(res => {
        this.$message({ message: '新增成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    },
    // 编辑
    handleToEdit() {
      let ids = []
      for (var i = 0; i < this.lsShiftData.length; i++) {
        ids.push(this.lsShiftData[i].inventoryId)
      }
      this.form.ids = ids
      updateShift(this.form.planNo, this.form).then(res => {
        this.$message({ message: '编辑成功!', type: 'success' })
        this.dialogForm = false
        this.$emit('load')
      })
    },
    changeRelocationType(val) {
      if (val == 'A') {
        this.form.targetWarehouseCode = ''
        this.form.targetWarehouseName = ''
        this.form.targetRoomCode = ''
        this.form.targetRoomName = ''
        this.form.search_EQ_aulaNo = ''
        this.form.search_EQ_spanNo = ''
        this.typeDisabled = false
        this.typeDisabled1 = false
      }
      if (val == 'B') {
        this.form.targetWarehouseCode = this.form.warehouseCode
        this.form.targetWarehouseName = this.form.warehouseName
        this.form.targetRoomCode = ''
        this.form.targetRoomName = ''
        this.form.search_EQ_aulaNo = ''
        this.form.search_EQ_spanNo = ''
        this.typeDisabled = true
        this.typeDisabled1 = false
        this.changeWarehouse()
      }
      if (val == 'C') {
        this.form.targetWarehouseCode = this.form.warehouseCode
        this.form.targetWarehouseName = this.form.warehouseName
        this.form.targetRoomCode = this.form.roomCode
        this.form.targetRoomName = this.form.roomName
        this.typeDisabled = true
        this.typeDisabled1 = true
        this.changeTargetRoom()
      }
      console.log(val)
    },
    changeWarehouse(num) {
      if (num == '1') {
        for (var i = 0; i < this.warehouseList.length; i++) {
          if (this.warehouseList[i].name == this.condition.warehouseName) {
            getStorageSetList({
              'search_EQ_warehouse.code': this.warehouseList[i].code
            }).then(res => {
              this.storageSet1 = res.data.aaData
            })
          }
        }
      } else {
        this.form.targetRoomCode = ''
        this.form.targetRoomName = ''
        for (var i = 0; i < this.warehouseList.length; i++) {
          if (this.warehouseList[i].code == this.form.targetWarehouseCode) {
            this.form.targetWarehouseName = this.warehouseList[i].name
            this.form.targetWarehouseId = this.warehouseList[i].id
            getStorageSetList({
              'search_EQ_warehouse.code': this.warehouseList[i].code
            }).then(res => {
              this.storageSet = res.data.aaData
            })
          }
        }
      }
    },
    changeTargetRoom(num) {
      let that = this
      if (num == '1') {
        that.condition.aulaNo = ''
        that.condition.spanNo = ''
        for (var i = 0; i < that.storageSet1.length; i++) {
          if (that.storageSet1[i].roomCode == that.condition.roomName) {
            that.ktipArr1 = that.storageSet1[i].maxSpan
            that.jtipArr1 = that.storageSet1[i].maxAula
          }
        }
      } else {
        that.form.search_EQ_aulaNo = ''
        that.form.search_EQ_spanNo = ''
        for (var i = 0; i < that.storageSet.length; i++) {
          if (that.storageSet[i].roomName == that.form.targetRoomName) {
            that.form.targetRoomCode = that.storageSet[i].roomCode
            that.ktipArr = that.storageSet[i].maxSpan
            that.jtipArr = that.storageSet[i].maxAula
          }
        }
      }
    },
    getWarehouseName() {},
    //多选
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.removeIds.push(val[0].inventoryId)
    },
    handleRemove() {
      let that = this
      that
        .$confirm(`是否移除该数据?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          for (var i = 0; i < that.removeIds.length; i++) {
            for (var j = 0; j < that.lsShiftData.length; j++) {
              if (that.removeIds[i] == that.lsShiftData[j].inventoryId) {
                that.lsShiftData.splice(j)
              }
            }
          }
          that.$message({
            type: 'success',
            message: `移除成功!`
          })
        })
        .catch(() => {
          that.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    filterPamars(param) {
      let pam = {}
      for (let i in param) {
        if (param[i] != '') {
          pam[i] = param[i]
        }
      }
      return pam
    }
  },
  watch: {
    dialogForm(bool) {
      if (!bool) {
        this.lsShiftData = []
        this.shiftData = []
        // this.$refs.condition.resetFields()
        this.condition = this.clearForm()
        this.$emit('clear')
      } else {
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.box {
  border: 2px solid #eee;
  margin: 20px 0;
}
.box .title,
.box .rule {
  background: #eee;
  height: 50px;
  line-height: 50px;
  padding-left: 10px;
}
.block {
  margin: 20px 0;
}
.number {
  color: red;
}
</style>
